package com.example.calendarapp;

import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

/**
 * Class for editing a given category or creating a new one
 */

public class CategoryEditFragment extends Fragment{
	String[] colors = {"black", "blue", "green", "orange", "purple", "red"};
	EventsDataSource data;
	private Button saveBtn;
	private Button delBtn;
	private EditText nameET;
	private Spinner colorSpn;
	private boolean isNewCategory;
	private EventCategory cat;
	
	
	/**
	 * onCreateView simply inflates and sets the edittext and spinner and button adapters and onclick overrides
	 * checks if is a created event or a new event before deleting updates otherwise
	 * 
	 * @param LayoutInflater
	 * @param ViewGroup
	 * @param Bundle
	 * @return View
	 *
	 */
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		data = EventsDataSource.getInstance(getActivity());
		data.createDatabase();
		data.open();	
		
		View convertView = inflater.inflate(R.layout.categoryedit, null);
		saveBtn = (Button) convertView.findViewById(R.id.catSaveBtn);
		delBtn = (Button) convertView.findViewById(R.id.catDeleteBtn);
		nameET = (EditText) convertView.findViewById(R.id.catName);
		colorSpn = (Spinner) convertView.findViewById(R.id.catColor);
		
		colorSpn.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, colors));
		
		Bundle arguments = getArguments();
		
		if(arguments == null)
		{
			isNewCategory = true;
			delBtn.setEnabled(false);
		}
		else
		{
			isNewCategory = false;
			
		}
		
		if(!isNewCategory)
		{
			cat = data.getCategoryByPos(arguments.getInt("id"));
			nameET.setText(cat.getName());
			colorSpn.setSelection(cat.getColor());
		}
		
		saveBtn.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View arg0) {
				if(isNewCategory)
				{
					data.addCategory(nameET.getText().toString(), colorSpn.getSelectedItemPosition());
				}
				else
					data.updateCategory(cat.getId(), nameET.getText().toString(), colorSpn.getSelectedItemPosition());
				((ViewGroup)getView().getParent()).removeAllViews();
				getFragmentManager().popBackStack();
			}
			
		});
		
		delBtn.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View arg0) {
				if(!isNewCategory)
					if(cat.getId() > 4)
						data.deleteCategory(cat.getId());
					else
						Toast.makeText(getActivity(), "Can't delete default categories!", Toast.LENGTH_SHORT).show();
				((ViewGroup)getView().getParent()).removeAllViews();
				getFragmentManager().popBackStack();
			}
			
		});
		
		return convertView;
	}
	

	 
}
